package ipdesk.model.report.channel.action;

import ipdesk.model.report.channel.bean.ChannelInfoBean;
import ipdesk.model.report.channel.dao.ChannelInfoDao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.util.FileService;
import ecg.util.StringUtils;
import ecg.util.Time;

public class ChannelInfoRegAction extends ActionSupport implements Preparable, ModelDriven<ChannelInfoBean>
{
	private ChannelInfoBean channelInfoBean;
	FileService fileService = new FileService();
	
	public String execute() throws Exception
	{
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.channel");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		String uploadFileName = channelInfoBean.getUploadFileName();
		
		String realFileName = fileService.saveFile(channelInfoBean.getUpload(), totalPath, uploadFileName);
		
		channelInfoBean.setUploadFileName(realFileName);
		channelInfoBean.setPathName(basePath);
		channelInfoBean.setRegTime(regTime);
		channelInfoBean.setAttendCompany(StringUtils.nl2br(channelInfoBean.getAttendCompany()));
		channelInfoBean.setFuturePlan(StringUtils.nl2br(channelInfoBean.getFuturePlan()));
		channelInfoBean.setDiscussIssue(StringUtils.nl2br(channelInfoBean.getDiscussIssue()));
		channelInfoBean.setAttendSubject(StringUtils.nl2br(channelInfoBean.getAttendSubject()));
		
		
		
	
		// 협력구축채널정보 insert.
		int lastChannelSeq = ChannelInfoDao.getInstance().channelInfoReg(channelInfoBean);
	
		// 마지막으로 insert된 channel_seq 값을 빈 객체에 저장.		
		channelInfoBean.setChannelSeq(lastChannelSeq);
	
		if(lastChannelSeq != 0 && channelInfoBean.getUpload() != null)
		{
			ChannelInfoDao.getInstance().insertFileData(channelInfoBean);
		}
		
		return SUCCESS;
	}
	
	public void prepare() throws Exception
	{
		channelInfoBean = new ChannelInfoBean();
	}
	
	public ChannelInfoBean getModel()
	{
		return channelInfoBean;
	}
	
	/*public void validate()
	{
		String contentType = channelInfoBean.getUploadContentType();
		long fileSize = 0;
		
		if(channelInfoBean.getUpload() != null)
		{
			fileSize = channelInfoBean.getUpload().length();
		}
		
		if(!fileService.contentTypeValidate(contentType))
		{
			addFieldError("upload", "업로드할 수 없는 파일 형식입니다");
			return;
		}
		
		if(fileSize > Long.parseLong(getText("upload.size")))
		{
			addFieldError("upload", "업로드 용량을 초과하였습니다.");
			return;
		}
	}*/
}
